﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.PdaApi.Models.Request.Cabinet;
using IOA.MES.PdaApi.Models.Response.Cabinet;
using IOA.MES.PdaApi.Models.Response.Product;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// 装柜接口
    /// </summary>
    public class CabinetController : ApiController
    {
        /// <summary>
        /// 拼柜
        /// </summary>
        [HttpPost]
        public ResultJson<CabinetResponse> Cabinet([FromBody] CabinetRequest request)
        {
            var warehouse = BaseEnv.Warehouse;
            VCabinetCondition condition = null;
            new CabinetMgr().CabinetPack(new VCabinet
            {
                CabinetNo = request.CabinetNo,
                WarehouseCode = warehouse?.WarehouseCode,
                WarehouseName = warehouse?.WarehouseName,
                Status = CabinetStatus.Normal,
                CreateBy = BaseEnv.User.PkId
            }, request.PackNos, out condition);

            return new ResultJson<CabinetResponse>();
        }

        /// <summary>
        /// 解柜操作
        /// </summary>
        [HttpPost]
        public ResultJson<CancelCabinetResponse> CancelCabinet([FromBody] CancelCabinetRequest request)
        {
            new CabinetMgr().UnCabinet(new VCabinet
            {
                CabinetNo = request.CabinetNo,
                ModifyBy = BaseEnv.User.PkId
            });
            return new ResultJson<CancelCabinetResponse>();
        }

        /// <summary>
        /// 查询拼柜记录
        /// </summary>
        [HttpPost]
        public ResultJson<QueryCabinetsResponse> QueryCabinets([FromBody] QueryCabinetsRequest request)
        {
            var total = 0;
            var cabinets = new CabinetMgr().QueryCabinets(request.PageIndex, request.PageSize, out total, BaseEnv.User.PkId);

            return new ResultJson<QueryCabinetsResponse>(new QueryCabinetsResponse
            {
                Cabinets = cabinets.Select(_ => new Cabinet
                {
                    WarehouseCode = _.WarehouseCode,
                    WarehouseName = _.WarehouseName,
                    CabinetBy = _.CNCreateBy,
                    CabinetNo = _.CabinetNo,
                    CabinetTime = _.CreateTime,
                    PackQuantity = _.PackQuantity,
                    Packs = new List<PackItem>()
                }).ToList()
            }, total);
        }

        /// <summary>
        /// 获取拼柜对象
        /// </summary>
        [HttpPost]
        public ResultJson<GetCabinetResponse> GetCabinet([FromBody] GetCabinetRequest request)
        {
            var cabinet = new CabinetMgr().GetCabinet(request.CabinetNo);

            return new ResultJson<GetCabinetResponse>(new GetCabinetResponse
            {
                Cabinet = cabinet == null ? null : new Cabinet
                {
                    WarehouseCode = cabinet.WarehouseCode,
                    WarehouseName = cabinet.WarehouseName,
                    CabinetBy = cabinet.CNCreateBy,
                    CabinetNo = cabinet.CabinetNo,
                    CabinetTime = cabinet.CreateTime,
                    PackQuantity = cabinet.PackQuantity,
                    Packs = cabinet.Packs.Select(_ => new PackItem
                    {
                        Location = _.Location,
                        OrderNo = _.OrderNo,
                        PackBatchNo = _.PackBatchNo,
                        PackNo = _.PackNo,
                        WorkOrderNo = _.WorkOrderNo
                    }).ToList()
                }
            });
        }
    }
}
